﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Web.Utility;
using System.Data;
using BySoft.Server.Common.Entity;
using BySoft.Server.Business;
using BySoft.Server.Common.Enum;
using BySoft.Server.Common;
using OneSoft.EntityCore;
using BySoft.Server.Consts;
using BySoft.Server.Common.Schema;

namespace BySoft.Web.Site.SysManage
{
    public partial class UserEdit :  AuthBasePage
    {
        protected DataTable DtProcessType
        {
            get
            {
                if (Session["Member_DtProcessType"] != null)
                {
                    return (DataTable)Session["Member_DtProcessType"];
                }
                else
                {
                    return CreateDT();
                }
            }
            set
            {
                Session["Member_DtProcessType"] = value;
            }
        }

        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("NoStandardPartsTypeDetailID", typeof(Int32));//0
            dt.Columns.Add("ProcessNO");//0
            dt.Columns.Add("ProcessLineID");//0
            dt.Columns.Add("ProcessLineName");//0
            return dt;
        }
        string m_userId = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            m_userId = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("USER_ID"));
            if(!IsPostBack)
            {
                Session["Member_DtProcessType"] = null;
                //部门
                BindParameterForListControl("Department", ddlDepartmentID);
                UIHelper.InsertFirstItem(ddlDepartmentID);
                //DataTable dtProcessDetail = MdProcessline.SelectData("");

                //ddlProcessName.DataSource = dtProcessDetail;
                //ddlProcessName.DataTextField = "ProcessLineName";//"PARAMETERNAME"; 
                //ddlProcessName.DataValueField = "ProcessLineID";// "C_PARAMETERCODE";
                //ddlProcessName.DataBind();
                //UIHelper.InsertFirstItem(ddlProcessName);
                //InitData(m_userId);
            }
            if (!IsPostBack)
            {
                InitControl();
                SysUserEntity oSysUser = SysUser.GetSysUserByID(m_userId);
                if (oSysUser != null)
                {
                    objSysUser = oSysUser;
                    lblUserId.Text = oSysUser.USERLOGINID;
                    txtUserName.Text = oSysUser.UserName;
                    ddlUserType.SelectedValue = oSysUser.EmployeeType;
                    ddlDepartmentID.SelectedValue = oSysUser.DepartmentID;
                    if (oSysUser.IsGeneral.Equals("1"))
                    {
                        chkIsGeneral.Checked = true;
                    }

                    for (int i = 0; i < ddlStatus.Items.Count; i++)
                    {
                        if (ddlStatus.Items[i].Value == oSysUser.Status)
                        {
                            ddlStatus.SelectedIndex = i;
                            break;
                        }
                    }

                    if (oSysUser.CanUseRole.Equals("1"))
                    {
                        chbCanUserRole.Checked = true;
                    }

                    lblCreateUser.Text = SysUser.GetName(oSysUser.CreateUserID);
                    if (oSysUser.CreateDate.Value != null)
                    {
                        lblCreateTime.Text = oSysUser.CreateDate.Value.ToString("yyyy-MM-dd");
                    }
                    lblUpdateUser.Text = SysUser.GetName(oSysUser.ModifyUserID);
                    if (oSysUser.ModifyDate.Value != null)
                    {
                        lblUpdateTime.Text = oSysUser.ModifyDate.Value.ToString("yyyy-MM-dd");
                    }

                    //用户角色
                    DataTable dtRole = SysUserrole.GetRoleByUser(m_userId);
                    if (dtRole.Rows.Count > 0)
                    {
                        sgvRole.DataSource = dtRole;
                        sgvRole.DataBind();
                    }
                    else
                    {
                        UIHelper.BindEmptyGridView(sgvRole, dtRole);
                    }
                    //用户权限
                    DataTable dtRight = SysUserrole.GetRoleRightsByUser(m_userId);
                    if (dtRight.Rows.Count > 0)
                    {
                        sgvRight.DataSource = dtRight;
                        sgvRight.DataBind();
                    }
                    else
                    {
                        UIHelper.BindEmptyGridView(sgvRight, dtRight);
                    }
                }
            }

        }
        private void InitControl()
        {
            //用户类型
            UIHelper.BindSortList(ddlUserType, BaseEnum.UserTypeList2());
            UIHelper.InsertFirstItem(ddlUserType);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            SysUserEntity oSysUser = new SysUserEntity();
            oSysUser.UserID = m_userId;
            oSysUser.UserName = txtUserName.Text.Trim();
            oSysUser.UserNamePinyin = CrazyCoderPinyin.Convert(oSysUser.UserName).ToLower();
            oSysUser.UserNamePYFirstChar = GetFirstZiMu.GetSpellCode(oSysUser.UserName).ToLower();
            oSysUser.DepartmentID = ddlDepartmentID.SelectedValue.Trim();
            oSysUser.IsGeneral = chkIsGeneral.Checked ? "1" : "0";
            oSysUser.CanUseRole = chbCanUserRole.Checked ? "1" : "0";

            oSysUser.USERLOGINID = objSysUser.USERLOGINID;

            oSysUser.Status = ddlStatus.SelectedItem.Value;
            oSysUser.Password = objSysUser.Password;
            if (objSysUser.PasswordChangeDate != DateTime.MinValue)
            {
                oSysUser.PasswordChangeDate = objSysUser.PasswordChangeDate;
            }
            oSysUser.IsDefaultPwd = objSysUser.IsDefaultPwd;
            if (objSysUser.LastestLoginDate != DateTime.MinValue)
            {
                oSysUser.LastestLoginDate = objSysUser.LastestLoginDate;
            }
            oSysUser.CreateUserID = objSysUser.UserID;
            oSysUser.CreateDate = DateTime.Parse(lblCreateTime.Text);
            oSysUser.ModifyUserID = CurrentUser.UserID;
            oSysUser.ModifyDate = DateTime.Now;

            List<SysUserprocessEntity> listy = new List<SysUserprocessEntity>();
            foreach (DataRow dr in DtProcessType.Rows)
            {
                SysUserprocessEntity Detailentity = new SysUserprocessEntity();
                Detailentity.ProcessLineID = Convert.ToInt32(dr["ProcessLineID"].ToString().Trim());
                Detailentity.UserID = m_userId;
                listy.Add(Detailentity);
            }
            if (SysUser.UpdateSysUser(oSysUser))
            {
                //SysUser.UserProcessSaveOrUpdate(m_userId,listy, "UpData");
                base.LogAction(BaseEnum.ActionType.Modify, oSysUser.UserID, true, null, new EntityBase[] { oSysUser });

                UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "/SysManage/UserList.aspx");
            }
            else
            {
                base.LogAction(BaseEnum.ActionType.Modify, oSysUser.UserID, false, null, new EntityBase[] { oSysUser });

                UIHelper.AlertMessage(this.Page, Message.SaveFail);
            }

        }

        protected SysUserEntity objSysUser
        {
            get
            {
                if (ViewState["objSysUser"] != null)
                {
                    return (SysUserEntity)ViewState["objSysUser"];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                ViewState["objSysUser"] = value;
            }
        }

        //void InitData(string sUserId)
        //{
        //    DtProcessType = CreateDT();
        //    DataTable dtdetail = SysUser.SelectUserProcess(sUserId);
        //    DataTable dtTemp = DtProcessType.Copy();
        //    if (dtdetail != null && dtdetail.Rows.Count > 0)
        //    {
        //        for (int i = 0; i < dtdetail.Rows.Count; i++)
        //        {

        //            DataRow drNew = dtTemp.NewRow();
        //            drNew["NoStandardPartsTypeDetailID"] = i;
        //            drNew["ProcessNO"] = "OP" + (i + 1).ToString();
        //            drNew["ProcessLineID"] = dtdetail.Rows[i]["ProcessLineID"];
        //            drNew["ProcessLineName"] = dtdetail.Rows[i]["ProcessLineName"];
        //            //drNew["OrderNo"] = "";
        //            dtTemp.Rows.Add(drNew);
        //        }

        //        DtProcessType = dtTemp;
        //        BindData();
        //    }
        //}

        //protected void btnAdd_Click(object sender, EventArgs e)
        //{

        //    if (ddlProcessName.SelectedIndex != 0)
        //    {
        //        DataTable dtTemp = DtProcessType.Copy();
        //        DataRow[] drs = dtTemp.Select("ProcessLineID='"+ddlProcessName.SelectedItem.Value.Trim()+"'");
        //        if (drs != null && drs.Length>0)
        //        {
        //            UIHelper.AlertMessage(this.UpdatePanel2, "已存在该工序！");
        //            return;
        //        }

        //        DataRow NewProcessType = dtTemp.NewRow();
        //        NewProcessType["NoStandardPartsTypeDetailID"] = DtProcessType.Rows.Count;
        //        NewProcessType["ProcessNO"] = "OP" + (DtProcessType.Rows.Count + 1).ToString();
        //        NewProcessType["ProcessLineName"] = ddlProcessName.SelectedItem.Text.Trim();
        //        NewProcessType["ProcessLineID"] = ddlProcessName.SelectedItem.Value.Trim();
        //        dtTemp.Rows.Add(NewProcessType);
        //        DtProcessType = dtTemp;
        //        DtProcessType.AcceptChanges();
        //        BindData();
        //    }
        //    else
        //    {
        //        UIHelper.AlertMessage(this.UpdatePanel2, "请选择工序！");
        //    }

        //    ddlProcessName.SelectedIndex = 0;
        //    UIHelper.RunScript(this, "iframeAutoFit();");
        //}
        //private void BindData()
        //{
        //    DataView dv = new DataView(DtProcessType);
        //    dv.Sort = "NoStandardPartsTypeDetailID";
        //    DataTable dtProcessDetail = dv.ToTable();
        //    if (dtProcessDetail.Rows.Count > 0)
        //    {
        //        grdResult.DataSource = dtProcessDetail;
        //        grdResult.DataBind();
        //    }
        //    else
        //    {
        //        UIHelper.BindEmptyGridView(grdResult, dtProcessDetail);
        //    }
        //    UIHelper.RunScript(this.Page, "iframeAutoFit();");
        //}

        //#region Grid事件
        ///// <summary>
        /////   行事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
        //{
        //    string str = e.CommandArgument.ToString();
        //    int iDel = CommonMethod.ConvertToInt(str);
        //    DataTable dtTemp = new DataTable();
        //    dtTemp = DtProcessType.Copy();
        //    switch (e.CommandName)
        //    {
        //        case "del":
        //            DataTable dtProcessDetail = DtProcessType.Copy();
        //            dtProcessDetail.Rows.RemoveAt(iDel);
        //            DtProcessType = dtProcessDetail;
        //            for (int i = 0; i < DtProcessType.Rows.Count; i++)
        //            {
        //                DtProcessType.Rows[i]["NoStandardPartsTypeDetailID"] = i;
        //                DtProcessType.Rows[i]["ProcessNO"] = "OP" + (i + 1).ToString();
        //            }
        //            DtProcessType.AcceptChanges();
        //            BindData();
        //            UIHelper.Alert(this.UpdatePanel2, Message.DeleteSuccess);
        //            break;
        //        default:
        //            break;
        //    }
        //}

        ///// <summary>
        ///// 行数据绑定
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {
        //        #region 删除绑定
        //        ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
        //        Button btnDel = (Button)e.Row.FindControl("btnDel");
        //        if (ibtn != null)
        //        {
        //            ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
        //        }
        //        #endregion
        //    }
        //}
        //#endregion
    }
}
